﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace oop4
{
    internal class ContactSystem : Contact
    {
        private static List<Contact> contacts = new List<Contact>();

        public static void EditContact()
        {
            Console.Write("请输入要修改的联系人: ");
            string name = Console.ReadLine();
            var contact = contacts.Find(x => x.Name == name);
            if (contact != null)
            {
                Console.WriteLine("请输入新的姓名: ");
                contact.Name = Console.ReadLine();
                Console.WriteLine("请输入新的电话号码: ");
                contact.Name = Console.ReadLine();
            }

        }
        public static void deleteContact()
        {
            Console.Write("请输入要删除的联系人的姓名: ");
            string name = Console.ReadLine();
            var contact = contacts.Find(x => x.Name == name);
            if(contact != null)
            {
                contacts.Remove(contact);
                Console.WriteLine("联系人删除成功!");
            }
            else
            {
                Console.WriteLine("未找到要删除的联系人!");
            }

        }
        public static void addContact()
        {
            Console.Write("请输出姓名: ");
            string name = Console.ReadLine();
            Console.Write("请输出电话号码: ");
            string phoneNumber = Console.ReadLine();

            contacts.Add(new Contact { Name = name, PhoneNumber = phoneNumber });
            Console.WriteLine("联系人已添加!");
        }
        public static void searchContact(string num)
        {
            if(num == "4")
            {
                Console.Write("请输入要查找的联系人的姓名: ");
                string name = Console.ReadLine();
                var contact = contacts.Find(x => x.Name == name);
                if (contact != null)
                {
                    contact.printContact();
                }
                else
                {
                    Console.WriteLine("未找到该联系人!");
                }
            }
            else if(num == "5") 
            {
                foreach(var contact in contacts)
                {
                    contact.printContact();
                }
            }
        }
        public static void saveContact()
        {
            // 保存联系人到 文件
            string path = @"D:\ContactPerson\text.dat";
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
            {
                foreach (var contact in contacts)
                {
                    string serializeString = JsonSerializer.Serialize(contact);
                    BinaryWriter bw = new BinaryWriter(fs, Encoding.UTF8, false);
                    bw.Write(serializeString);
                }
                fs.Close();
            }
        }
        public static void loadContact()
        {
            string path = @"D:\ContactPerson\text.dat";
            if (File.Exists(path))
            {
                using (FileStream reader = new FileStream(path, FileMode.Open))
                {
                    BinaryReader rw = new BinaryReader(reader, Encoding.UTF8, false);
                    Contact contact = new Contact();
                    contact.Name = rw.ReadString();
                    //contact.PhoneNumber = rw.ReadString();
                    contacts.Add(contact);
                }

            }
        }
    }
}
